﻿using System;
using System.Collections;
using System.Drawing;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Telerik.Web.UI;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GBMS.APP_UI.UserControls;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BGM_Project_Control_Expense : System.Web.UI.Page
    {
        private IProject projectBLL = new Project_BLL();
        private ISourceBLL src = new SourceBLL();
        private IExpenseGroupBLL expGrp = new ExpenseGroupBLL();
        private Expense_BLL exp = new Expense_BLL();
        private ProjectControl_BLL ctrl = new ProjectControl_BLL();
        private ComboBLL cmb = new ComboBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lbProjectID.Text = Request.QueryString["hid"];
                //litTitle.Text = Request.QueryString["hname"];
                //lbProjectName.Text = Request.QueryString["hname"];

                LoadHeader();
                LoadSource();
                LoadData();
                LoadProjectControl();
                UpdateUI("View");
            }
        }

        private void LoadHeader()
        {
            string strBgmProjSeq = lbProjectID.Text;
            DataTable dtb = projectBLL.GetDataProjectBySequence(strBgmProjSeq);

            if (dtb.Rows.Count > 0)
            {
                lbSubActName.Text = dtb.Rows[0]["bgm_strat_list_name"].ToString();
                lbJobsiteName.Text = dtb.Rows[0]["job_site_name"].ToString();
                hSubActSeq.Value = dtb.Rows[0]["bgm_strat_list_seq"].ToString();
                hJobsiteSeq.Value = dtb.Rows[0]["job_site_seq"].ToString();
                hBgmYear.Value = dtb.Rows[0]["bgm_year"].ToString();
                litTitle.Text = dtb.Rows[0]["bgm_prj_name"].ToString();
                lbProjectName.Text = dtb.Rows[0]["bgm_prj_name"].ToString();
            }
        }

        private void LoadData()
        {
            string strProjectSeq = lbProjectID.Text;
            string strSourceSeq = cbSource.SelectedValue;
            DataTable dtb = projectBLL.GetDataProjectReportExpense(strProjectSeq, strSourceSeq);

            gridExpense.DataSource = dtb;
            gridExpense.DataBind();
        }

        private void LoadProjectControl()
        {
            string strBgmYear = hBgmYear.Value;
            string strBgmStratListSeq = hSubActSeq.Value;
            string strBgmSourceSeq = cbSource.SelectedValue;
            string strJobsiteSeq = hJobsiteSeq.Value;

            rpProjControl.DataSource = ctrl.GetFull(strBgmYear, strBgmStratListSeq, strBgmSourceSeq, strJobsiteSeq, "");
            rpProjControl.DataBind();
        }

        private void LoadSource()
        {
            cbSource.DataSource = cmb.ListSourceCombo();
            cbSource.DataBind();

            cbSource.SelectedIndex = 0;
        }

        protected void cbSource_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadData();
            LoadProjectControl();
            UpdateUI("View");
        }

        protected void gridExpense_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            string strProjectSeq = lbProjectID.Text;
            string strSourceSeq = cbSource.SelectedValue;
            DataTable dtb = projectBLL.GetDataProjectReportExpense(strProjectSeq, strSourceSeq);

            gridExpense.DataSource = dtb;
        }

        protected void RadToolTipManager1_AjaxUpdate(object sender, ToolTipUpdateEventArgs e)
        {
            int index = Convert.ToInt32(e.Value);
            string strMode = (e.TargetControlID.IndexOf("cmdPlus") >= 0 ? "Plus" : "Minus");
            GridDataItem item = gridExpense.Items[index];
            string strBgmExpGrpSeq = item["bgm_exp_grp_seq"].Text;

            UserControls.ProjectControl ctrl = (UserControls.ProjectControl)Page.LoadControl("../UserControls/ProjectControl.ascx");
            ctrl.LoadData(hBgmYear.Value, hSubActSeq.Value, cbSource.SelectedValue, hJobsiteSeq.Value
                , strBgmExpGrpSeq, strMode);

            e.UpdatePanel.ContentTemplateContainer.Controls.Add(ctrl);
        }

        protected void cmdInsert_Click(object sender, EventArgs e)
        {
            string strMode = (lbMode.Text == "ดึงเงินจากกองกลาง" ? "2" : "1");
            string strEmpSeq = new SsMng().GetUss(SsMng.SsUserType.EmpSeq);
            string strAmount = (strMode == "1" ? "" : "-") + txtTotalAmount.Text;
            string strCalAmount = (strMode == "1" ? "-" : "") + txtTotalAmount.Text;
            string strDescr = lbMode.Text;
            string strBgmExpGrpSeq = lbBgmExpGrpSeq.Text;
            //Label rplbAmount = (Label)rpProjControl.Items[i].FindControl("lbAmount");

            if (strMode == "2")//Plus
            {
                bool bOK = true;
                int i = 0;
                while (bOK || i < rpProjControl.Items.Count)
                {
                    HtmlInputHidden glbBgmExpGrpSeq = (HtmlInputHidden)rpProjControl.Items[i].FindControl("lbBgmExpGrpSeq");

                    if (glbBgmExpGrpSeq.Value == strBgmExpGrpSeq)
                    {
                        if (Convert.ToDouble(txtTotalAmount.Text) > Convert.ToDouble(((Label)rpProjControl.Items[i].FindControl("lbAmount")).Text))
                        {
                            lbError.Visible = true;
                            return;
                        }
                        bOK = false;
                    }
                    i++;
                }
            }
            else
            {
                //Label lbTotalAmount = gridExpense.Items[]

                if (Convert.ToDouble(txtTotalAmount.Text) > Convert.ToDouble(lbAmount.Text))
                {
                    lbError.Visible = true;
                    return;
                }
            }

            DataTable dtb = ctrl.FindProjectCtrl(hBgmYear.Value, hSubActSeq.Value, cbSource.SelectedValue
                , hJobsiteSeq.Value, strBgmExpGrpSeq);

            if (dtb.Rows.Count > 0)
            {
                lbBgmProjCtrlSeq.Text = dtb.Rows[0]["bgm_proj_ctrl_seq"].ToString();
            }
            else
            {
                lbBgmProjCtrlSeq.Text = ctrl.Insert_BgmProjectControl(hBgmYear.Value, cbSource.SelectedValue
                    , hSubActSeq.Value, hJobsiteSeq.Value, strBgmExpGrpSeq, strEmpSeq);
            }

            //projectBLL.UpdateTotalAmountProjectExpense();
            //ctrl.Insert_BgmProjectControlExpense();
            ctrl.Insert_BgmProjectControlExpense(lbBgmProjCtrlSeq.Text, strMode, lbBgmProjExpSeq.Text, strAmount, strEmpSeq);
            projectBLL.UpdateTotalAmountProjectExpense(lbBgmProjExpSeq.Text, strDescr, txtTotalAmount.Text, strCalAmount
                , (strMode == "1" ? "2" : "3"), strEmpSeq);

            LoadData();
            LoadProjectControl();
            UpdateUI("View");
        }

        protected void cmdCancel_Click(object sender, EventArgs e)
        {
            UpdateUI("View");
        }

        protected void gridExpense_ItemCommand(object source, GridCommandEventArgs e)
        {
            string strCmdName = e.CommandName;

            if (strCmdName == "Plus" || strCmdName == "Minus")
            {
                GridDataItem item = gridExpense.Items[e.Item.ItemIndex];
                lbBgmExpName.Text = item["bgm_exp_name"].Text;
                lbBgmExpGrpName.Text = item["bgm_exp_grp_name"].Text;
                lbBgmExpSeq.Text = item["bgm_exp_seq"].Text;
                lbBgmExpGrpSeq.Text = item["bgm_exp_grp_seq"].Text;
                lbBgmProjExpSeq.Text = item["bgm_proj_exp_seq"].Text;
                lbAmount.Text = ((Label)item["total_amount"].FindControl("lbTotalAmount")).Text;
                
                UpdateUI(strCmdName);
            }
        }

        protected void gridExpense_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem dataItem = (GridDataItem)e.Item;
                string strPrjTypeSeq = dataItem["prj_type_seq"].Text;
                if (strPrjTypeSeq == "4")
                {
                    Label lbTotalAmountOutPlan = (Label)dataItem["total_amount"].FindControl("lbTotalAmountOutPlan");
                    lbTotalAmountOutPlan.Visible = true;
                }
            }
        }

        private void UpdateUI(string strMode)
        {
            switch (strMode)
            {
                case "View" :
                    pnAdd.Visible = false;
                    break;
                case "Plus" :
                    pnAdd.Visible = true;
                    lbMode.Text = "ดึงเงินจากกองกลาง";
                    lbMode.ForeColor = Color.Green;
                    cmdInsert.Text = "ดึงเงินจากกองกลาง";
                    cmdInsert.ForeColor = Color.Green;
                    break;
                case "Minus" :
                    pnAdd.Visible = true;
                    lbMode.Text = "คืนเงินเข้ากองกลาง";
                    lbMode.ForeColor = Color.Red;
                    cmdInsert.Text = "คืนเงินเข้ากองกลาง";
                    cmdInsert.ForeColor = Color.Red;
                    break;
            }

            txtTotalAmount.Value = 0;
            lbError.Visible = false;
        }
    }
}
